#include <QFile>
#include <QTextStream>
#include <QtDebug>
#include "file_process_02.h"

#include "data_files/track_data_02.h"
#include "data_files/constant_variable.h"

File_process_02::File_process_02(QObject *parent) : QObject(parent)
{

}

File_process_02::~File_process_02()
{

}

void File_process_02::sl_open_files(QStringList file_names_list)
{
    QString file_name_wiht_path("");

    // 遍历文件名列表
    foreach(file_name_wiht_path, file_names_list) {
        QFile file(file_name_wiht_path);

        // 文件不存在
        if ( !file.exists()) {
            continue;
        }

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            in.setAutoDetectUnicode(true);

            QString content("");

            int row(0);
            // 按行读取文件内容，处理文件数据
            while ( 0 != (content = in.readLine()).length()) {

                emit si_send_track_to_model(get_track_data(content, row));
                ++row;
            }
        }
    }
}

void File_process_02::sl_split_file_data(QStringList file_names_list)
{
    QString file_name_wiht_path;

    foreach(file_name_wiht_path, file_names_list) {
        QFile file(file_name_wiht_path);
        if ( !file.exists()) {
            continue;
        }

        if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
            QTextStream in(&file);
            // 自动监测Unicode
            in.setAutoDetectUnicode(true);

            // Create two new files to save data
            QFile out_file_process(file_name_wiht_path.replace(".log", "_PROCESS_.log",
                                                               Qt::CaseInsensitive));
            QFile out_file_received_message(file_name_wiht_path.replace("_PROCESS_.log", "_MESSAGE_.log",
                                                                        Qt::CaseInsensitive));

            // open two files with write only and truncate pattern
            if (out_file_process.open(QIODevice::WriteOnly | QIODevice::Truncate) &&
                            out_file_received_message.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
                QTextStream out_stream_process(&out_file_process);
                QTextStream out_stream_message(&out_file_received_message);

                QString content;
                // Read data by lines and save data to files
                while (0 != (content = in.readLine()).length()) {
                    if (content.contains("sysRecv 1 received message")) {
                        out_stream_message<< content.replace("sysRecv 1 received message", "") << Qt::endl;
                    } else if (content.contains("sysRecv 1 process")) {
                        out_stream_process<< content.replace("sysRecv 1 process ", "") << Qt::endl;
                    }
                }
            }
        }
    }
}

Track_data_02 *File_process_02::get_track_data(QString content, int row)
{
    Track_data_02* data(new Track_data_02());

    QString data_time(content.mid(0, 8));
    data->get_data()->insert(TIME.trimmed(), data_time.trimmed());

    QStringList list = content.mid(11).split(",");
    QStringList key_value_group;
    QStringList::Iterator iter;

    // Traverse list, save key/value to Track_data_02 variable
    for (iter = list.begin(); list.end() != iter; ++iter) {
        key_value_group = iter->split("=");

        //
        if (2 == key_value_group.count()) {
            if (key_value_group[1].isEmpty()) {
                key_value_group[1] = "None";
            }

            // 将key/value保存到QMap中，去除数据的空格。将航班号的方括号去除
            data->get_data()->insert(key_value_group[0].trimmed(),
                    key_value_group[1].trimmed().replace("[", "").replace("]", ""));
        }
    }

    // Show flight track in Model/View
    data->set_show_track(true);
    data->set_row(row);

    return data;
}
